package instantHelper.Raceway;

import java.io.File;
import java.util.Date;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.*;

public class Synchronize extends Activity {
	ProgressDialog dialog;
	int increment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.synchronize);

		TextView tvAudio = (TextView) findViewById(R.id.tvValueAudio);
		TextView tvPicture = (TextView) findViewById(R.id.tvValuePicture);
		TextView tvVideo = (TextView) findViewById(R.id.tvValueVideo);
		tvAudio.setText(countUnsendData("audio") + " audios");
		tvPicture.setText(countUnsendData("picture") + " pictures");
		tvVideo.setText(countUnsendData("video") + " videos");
	}

	public void btnCloseClick(View button) {
		finish();
	}

	public void btnSyncClick(View button) {
		increment = 1;

		dialog = new ProgressDialog(this);
		dialog.setCancelable(true);
		dialog.setMessage("Loading...");
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		dialog.setProgress(0);
		int maximum = countUnsendAllData();
		dialog.setMax(maximum);
		dialog.show();

		Thread background = new Thread(new Runnable() {
			public void run() {
				Cursor cursor;
				DbHelper myDbHelper = new DbHelper(Synchronize.this);
				SQLiteDatabase db = myDbHelper.getWritableDatabase();
				try {
					String selection = InstantHelperConstant.DB_DIRECTORY_ISSEND
							+ "=0";
					cursor = db.query(InstantHelperConstant.DB_DIRECTORY_TABLE,
							null, selection, null, null, null, null, null);
					startManagingCursor(cursor);
					while (cursor.moveToNext()) {
						String path = cursor.getString(2);
						String requestId = cursor.getString(3);
						String fileType = cursor.getString(4);
						File file = new File(path);

						if (file.exists()) {
							fileType = fileType + "s";
							Boolean issend = FTPHelper.sendToServer(path,
									fileType);
							if (issend) {
								ContentValues contentValues = new ContentValues();
								Date time = new Date();
								contentValues.put(
										InstantHelperConstant.DB_COLUMN_ID,
										cursor.getString(0));
								contentValues
										.put(InstantHelperConstant.DB_DIRECTORY_DATE,
												time.toString());
								contentValues
										.put(InstantHelperConstant.DB_DIRECTORY_ISSEND,
												"1");
								contentValues
										.put(InstantHelperConstant.DB_DIRECTORY_PATH,
												cursor.getString(2));
								contentValues
										.put(InstantHelperConstant.DB_DIRECTORY_REQUEST_ID,
												cursor.getString(3));
								contentValues
										.put(InstantHelperConstant.DB_DIRECTORY_TYPE,
												cursor.getString(4));
								// update
								DbHelper myDbHelper1 = new DbHelper(
										Synchronize.this);
								SQLiteDatabase db1 = myDbHelper1
										.getWritableDatabase();
								String whereClause = InstantHelperConstant.DB_COLUMN_ID
										+ "=" + cursor.getString(0);
								db1.update(
										InstantHelperConstant.DB_DIRECTORY_TABLE,
										contentValues, whereClause, null);

							}
						} else {
							DbHelper myDbHelper1 = new DbHelper(
									Synchronize.this);
							SQLiteDatabase db1 = myDbHelper1
									.getWritableDatabase();
							String whereClause = InstantHelperConstant.DB_COLUMN_ID
									+ "=" + cursor.getString(0);
							db1.delete(
									InstantHelperConstant.DB_DIRECTORY_TABLE,
									whereClause, null);
						}
						progressHandler.sendMessage(progressHandler
								.obtainMessage());
					}
					myDbHelper.close();
					db.close();

				} catch (Exception e) {

				}
			}
		});
		background.start();
	}

	Handler progressHandler = new Handler() {
		public void handleMessage(Message msg) {
			dialog.incrementProgressBy(increment);
		}
	};

	private int countUnsendData(String dataType) {
		Cursor cursor;
		DbHelper myDbHelper = new DbHelper(Synchronize.this);
		SQLiteDatabase db = myDbHelper.getReadableDatabase();
		int rowCount = 0;
		try {
			String selection = InstantHelperConstant.DB_DIRECTORY_ISSEND
					+ "='0' and " + InstantHelperConstant.DB_DIRECTORY_TYPE
					+ "='" + dataType + "'";
			cursor = db.query(InstantHelperConstant.DB_DIRECTORY_TABLE, null,
					selection, null, null, null, null, null);
			startManagingCursor(cursor);
			rowCount = cursor.getCount();
			cursor.close();
			db.close();
			myDbHelper.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rowCount;
	}

	private int countUnsendAllData() {
		Cursor cursor;
		DbHelper myDbHelper = new DbHelper(this);
		SQLiteDatabase db = myDbHelper.getReadableDatabase();
		int rowCount = 0;
		try {
			String selection = InstantHelperConstant.DB_DIRECTORY_ISSEND + "=0";
			cursor = db.query(InstantHelperConstant.DB_DIRECTORY_TABLE, null,
					selection, null, null, null, null, null);
			startManagingCursor(cursor);
			rowCount = cursor.getCount();
			cursor.close();
			db.close();
			myDbHelper.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return rowCount;
	}

}
